#include "libutilsconfig.h"
#include "tconfigsetting.h"
#include "ustringutils.h"
#define  TIXML_USE_STL
#include "tinyxml.h"


LIB_NS_BEGIN
TConfigSetting::TConfigSetting()
{
}

TConfigSetting::~TConfigSetting(void)
{
}



bool TConfigSetting::LoadConfig( const char* fname )
{
	m_doc = shared_ptr<TiXmlDocument>(new TiXmlDocument);
	bool ret = m_doc->LoadFile(fname);
	m_fname = UStrConvUtils::ToTS(fname);
	return ret;
	
}

bool TConfigSetting::LoadConfig( const wchar_t* fname )
{
	std::string sname = UStrConvUtils::ToStr(fname);
	bool ret = LoadConfig(sname.c_str());
	return ret;
}



TiXmlNode& TConfigSetting::operator[]( const char*  key )
{
	TiXmlNode* pnode = m_doc->FirstChild(key);
	if(pnode == NULL)
	{
		std::string msg = std::string("the node ") + m_doc->ValueStr() + " element: " + key + " not found";

		std::cout<< msg<<std::endl;
		throw std::exception(msg.c_str());

	}

	return *pnode;
}

bool TConfigSetting::Save( const TCHAR* fname /*= NULL*/ )
{
	tstring name = m_fname;
	if(fname != NULL)
	{
		name = fname;
	}

	std::string asciiname = UStrConvUtils::ToStr(name);
	bool ret = m_doc->SaveFile(asciiname);
	return ret;
}

TiXmlNode& TConfigSetting::AppSetting()
{
	return *m_doc->FirstChild("configuration");
}


LIB_NS_END